package com.test21.nowcoder;

import java.util.Arrays;

public class Coder {
	public static void main(String[] args) {
		String[] strArr = { "i am a coder", "Coder Coder", "Code" };
		System.out.println(Arrays.toString(new Coder().findCoder(strArr, 3)));

	}

	/**
	 * 
	 * @param A
	 * @param n
	 * @return
	 */
	public String[] findCoder(String[] A, int n) {
		String key = "Coder".toLowerCase();
		int[][] tmpArr = new int[A.length][2];
		for (int i = 0; i < A.length; i++) {
			tmpArr[i][0] = i;
			tmpArr[i][1] = contentCount(A[i], key);
		}
		tmpArr = sort(tmpArr);
		String[] newArr = new String[A.length];
		for (int i = 0; i < tmpArr.length; i++) {
			newArr[i] = A[tmpArr[i][0]];
		}
		return newArr;
	}

	/**
	 * 计算包含数量
	 * @param src
	 * @param key
	 * @return
	 */
	public int contentCount(String src, String key) {
		String tmp = src.toLowerCase().replace(key, "");
		return (src.length() - tmp.length()) / key.length();
	}

	/**
	 * 排序
	 * @param tmpArr
	 * @return
	 */
	public int[][] sort(int[][] tmpArr) {
		for (int i = 0; i < tmpArr.length - 1; i++) {
			for (int j = 0; j < tmpArr.length - 1 - i; j++) {
				if (tmpArr[j][1] < tmpArr[j + 1][1]) {
					int[] tmp = tmpArr[j];
					tmpArr[j] = tmpArr[j + 1];
					tmpArr[j + 1] = tmp;
				}
			}
		}
		return tmpArr;
	}
}